import { combineReducers } from 'redux'
import {
  ERROR_MSG,
  AUTH_SUCCESS,
  RECEIVE_USER,
  REST_USER,
  RECEIVE_ICON,
  RECEIVE_USER_LIST
} from './action-type'
import { getRedirectPath } from '../utils'

const initUser = {
  username: '',
  type: '',
  msg: '',
  redirectTo: '',
  initIcon: []
}
const initUserList = []
function user(state = initUser, action) {
  switch (action.type) {
    case AUTH_SUCCESS:
      const redirectTo = getRedirectPath(action.data.type, action.data.header)
      return { ...action.data, redirectTo }
    case ERROR_MSG:
      return { ...state, msg: action.data }
    case RECEIVE_USER:
      return action.data
    case REST_USER:
      return { ...initUser }
    case RECEIVE_ICON:
      return { initIcon: [...action.data] }
    default:
      return state
  }
}
function userList(state = initUserList, action) {
  switch (action.type) {
    case RECEIVE_USER_LIST:
      return action.data
    default:
      return state
  }
}
export default combineReducers({
  user,
  userList
})
